package com.destinomusical.epura;

import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.destinomusical.epura.adapter.GridAdapter;
import com.destinomusical.epura.base.BaseActivity;
import com.destinomusical.epura.db.ControlBD;
import com.destinomusical.epura.obj.Usuario;
import com.smartthinking.destinomusical.R;

public class MainActivity extends BaseActivity {
	GridView options;
	boolean logedIn = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		options = (GridView) findViewById(R.id.options);
		options.setAdapter(getIconsAdapter());
		options.setOnItemClickListener(gridClick);
		Usuario usuario = getIntent().getParcelableExtra("usuario");
		setLogin(usuario);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		finish();
		return true;
	}

	OnItemClickListener gridClick = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			Intent intent = new Intent();
			intent.putExtra("usuario", usuario);

			switch (position) {
			case 0:
				intent.setClass(getApplicationContext(), Explorar.class);
				break;
			case 1:
				intent.setClass(getApplicationContext(), Categorias.class);
				break;
			case 2:
				intent.setClass(getApplicationContext(),
						ListaReproduccion.class);
				break;
//			case 7:
//				intent.setClass(getApplicationContext(), MisSuscripciones.class);
//				break;
			case 4:
				intent.setClass(getApplicationContext(), MisVideos.class);
				break;
			case 5:
				intent.setClass(getApplicationContext(), CatalogoCancion.class);
				break;
			case 6:
				intent.setClass(getApplicationContext(), Configuracion.class);

				break;
			case 3:
				intent.setClass(getApplicationContext(), Cupones.class);

				break;

			default:
				intent.setClass(getApplicationContext(), MainActivity.class);
				break;
			}

			if (usuario.isLogged() == 0 && (position > 1 && position < 5
					|| position == 7))
				Toast.makeText(getApplicationContext(),
						getString(R.string.msg_login), Toast.LENGTH_SHORT)
						.show();
			else if (position == 6) {
				startActivityForResult(intent, 2);
			} else {
				startActivity(intent);
			}
			// startActivity(intent);
		}

	};

	public GridAdapter getIconsAdapter() {
		GridAdapter adapter = null;
		String[] title = null;
		TypedArray icons = null;
		Resources res = getResources();
		title = res.getStringArray(R.array.opciones_titulo);
		icons = res.obtainTypedArray(R.array.opciones_imagen);
		if (title != null && icons != null)
			adapter = new GridAdapter(this, title, icons);
		return adapter;
	}

	public void onClickButtonLogin(View view) {
		if (logedIn) {
			logedIn = false;
			usuario.reset();
			ControlBD bd = new ControlBD(MainActivity.this);
			bd.deleteUser();
			((TextView) findViewById(R.id.user)).setText("");
			((Button) findViewById(R.id.login))
					.setText(getString(R.string.boton_login));
		} else {
			startActivityForResult(new Intent(getApplicationContext(),
					Login.class), 2);
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (data != null) {
			Usuario temp = data.getParcelableExtra("usuario");
			setLogin(temp);
		}
	}

	private void setLogin(Usuario usuario) {
		if (usuario.isLogged() == 1) {

			((Button) findViewById(R.id.login))
					.setText(getString(R.string.boton_logout));
			usuario.setLogged(1);
			this.usuario = usuario;

			((TextView) findViewById(R.id.user)).setText(usuario.getNombre());
			logedIn = true;
			ControlBD bd = new ControlBD(MainActivity.this);
			bd.insertUser(usuario);

		}
	}

}
